<template>
  <div class='container'>
<!-- 新增弹层 -->
    <el-dialog :title="Form.id? '修改文章': '新增文章'" :visible="dialogVisibleAdd" @close="guanbi">
      <el-form ref="form" label-width="80px"  :model="Form" :rules="rules">
        <el-form-item label="文章标题" prop="title">
          <el-input  placeholder="请输入文章标题" v-model="Form.title"/>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <!-- 富文本编辑器 -->
         <quill-editor :options="editorOption" v-model="Form.articleBody" @blur="$refs.addForm.validateField('articleBody')"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input placeholder="请输入视频地址" v-model="Form.videoURL"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="guanbi">取 消</el-button>
          <el-button type="primary" @click="addForm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { add, detail, update } from '@/api/hmmm/articles.js'

export default {
  naem: 'articlesAdd',
  components: {
    quillEditor
  },
  props: {
    dialogVisibleAdd: {
      type: Boolean
    }
  },
  data () {
    const toolbarOptions = [
      ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
      [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
      ['blockquote', 'code-block'], // 引用 代码块-----['blockquote', 'code-block']
      ['link', 'image', 'video'] // 链接、图片、视频-----['link', 'image', 'video']
    ]
    return {
      Form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ],
        videoURL: [
          // { required: true, message: '请输入视频地址', trigger: 'blur' }
        ]
      },
      // 富文本编辑器的配置
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions
          }
        }
      }
    }
  },
  mounted () {
    this.updateForm()
  },
  methods: {
    // 关闭
    guanbi () {
      this.$emit('guanbi')
      this.Form = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 确定
    addForm () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.Form.id) {
            // 修改
            await update(this.Form)
            this.$message.success('修改文章成功')
            this.$emit('addForm')
            this.guanbi()
          } else {
            // 新增
            await add(this.Form)
            this.$message.success('新增文章成功')
            this.$emit('addForm')
            this.guanbi()
          }
        } else {
        }
      })
    },
    // 修改详情
    async updateForm (row) {
      const res = await detail(row)
      this.Form = res.data
      // console.log(res)
    }
  }
}
</script>

<style scoped lang='css'>
.el-form-item::v-deep .ql-editor {
  height: 200px;
}
.el-form-item::v-deep .ql-toolbar.ql-snow {
  height: 38px;
  padding: 0px;
  padding-left: 8px;
}
</style>
